﻿using BattleFury.Lib.Bots;
using BattleFury.Lib.GameEvents;
using BattleFury.Lib.Graphics;
using BattleFury.Lib.Network;
using BattleFury.Lib.Network.Packets;
using BattleFury.Lib.ServerPlayer;
using Lidgren.Network;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Concurrent;

namespace BattleFury.Lib.Players
{
    public class Player : GenericPlayer, IEntity
    {

        private Texture2D _whitePixel;

        //public Engine Engine { get; set; }
        public Texture2D Texture { get; set; }
        public bool IsEnabled { get; set; }
        public bool IsVisible { get; set; }
        public float Speed { get; set; }
        public FacingDirection FacingDirection { get; set; }
        public Color Tint { get; set; }
        public int KeyFrame { get; set; }
        public bool ShowBounds { get; set; }
        public Func<long, Rectangle, bool> CollisionCheck { get; set; }


        //public event MovementChangedEventHandler MovementChanged;

        public NetworkClient NetworkClient
        {
            get { return RenderEngine.Get().NetworkClient; }
        }

        public SpriteBatch SpriteBatch
        {
            get { return RenderEngine.Get().SpriteBatch; }
        }


        public Player(long Id)
            : base(Id)
        {
            IsEnabled = true;
            IsVisible = true;
            Tint = Color.White;
            Speed = 4f;
            FacingDirection = FacingDirection.South;
        }

        public virtual void LoadContent()
        {
            _whitePixel = RenderEngine.Get().Textures["WhitePixel"];
        }

        public void Update(GameTime gameTime)
        {
            if (!IsEnabled) return;
            UpdateInput(gameTime);
        }

        public void UpdateInput(GameTime gameTime)
        {
            var input = RenderEngine.Get().Input;

            if (input.Keyboard.IsKeyHeld(Keys.W) || input.Keyboard.IsKeyHeld(Keys.Up))
            {
                //Move(new Movement() { Direction = 0.0f, Velocity = 1.0f});
                Move(new Movement(Position, new Vector2(Position.X, Position.Y - 5)));

                FacingDirection = FacingDirection.North;
                AdvanceKeyFrame();
            }
            if (input.Keyboard.IsKeyHeld(Keys.S) || input.Keyboard.IsKeyHeld(Keys.Down))
            {
                //Move(new Movement() { Direction = 180f, Velocity = 1.0f });
                Move(new Movement(Position, new Vector2(Position.X, Position.Y + 5)));

                FacingDirection = FacingDirection.South;
                AdvanceKeyFrame();
            }
            if (input.Keyboard.IsKeyHeld(Keys.A) || input.Keyboard.IsKeyHeld(Keys.Left))
            {
                //Move(new Movement() { Direction = 270f, Velocity = 1.0f });
                Move(new Movement(Position, new Vector2(Position.X - 5, Position.Y)));

                FacingDirection = FacingDirection.West;
                AdvanceKeyFrame();
            }
            if (input.Keyboard.IsKeyHeld(Keys.D) || input.Keyboard.IsKeyHeld(Keys.Right))
            {
                //Move(new Movement() { Direction = 90f, Velocity = 1.0f });
                Move(new Movement(Position, new Vector2(Position.X + 5, Position.Y)));

                FacingDirection = FacingDirection.East;
                AdvanceKeyFrame();
            }
        }

        private void AdvanceKeyFrame()
        {
            KeyFrame++;
            if (KeyFrame > 2) KeyFrame = 0;
        }

        public void Draw(GameTime gameTime)
        {
            if (!IsVisible) return;

            var srcRect = new Rectangle((int)(KeyFrame * Size.X), (int)((int)FacingDirection * Size.Y), (int)Size.X, (int)Size.Y);

            SpriteBatch.Draw(Texture, Position, srcRect, Tint);

            if (ShowBounds) SpriteBatch.Draw(_whitePixel, Bounds, Color.Red.WithOpacity(0.5f));
        }

        public void SendMessage(IPacket msg)
        {
            if (NetworkClient != null && NetworkClient.Status == NetConnectionStatus.Connected)
            {
                NetworkClient.SendPacket(msg);
            }
        }
        Vector2 IEntity.Position
        {
            get { return base.Position; }
            set { base.Position = value; }
        }

        Rectangle IEntity.Bounds
        {
            get { return base.Bounds; }
        }

        Vector2 IEntity.Size
        {
            get { return base.Size; }
            set { base.Size = value; }
        }



    }
}